package org.supreme.org.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.supreme.basic.result.JSONResult;
import org.supreme.basic.result.PageList;
import org.supreme.org.domain.Employee;
import org.supreme.org.query.DepartmentQuery;
import org.supreme.org.service.IDepartmentService;
import org.supreme.org.domain.Department;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.supreme.org.service.IEmployeeService;

import java.util.List;

@RestController
@RequestMapping("/department")
@Api
@RestControllerAdvice
public class DepartmentController {

    @Autowired
    public IDepartmentService departmentService;
    @Autowired
    private IEmployeeService employeeService;


    /**
    * 保存和修改公用的
    */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    @ApiOperation("保存的接口")
    public JSONResult saveOrUpdate(@RequestBody Department department){
        if(department.getId()!=null){
            departmentService.updateById(department);
        }else{
            departmentService.insert(department);
        }
        return JSONResult.success();
    }

    /**
    * 删除对象
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    @ApiOperation("删除接口")
    public JSONResult delete(@PathVariable("id") Long id){
        departmentService.deleteById(id);
        return JSONResult.success();
    }

    /**
   * 获取对象
   */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    @ApiOperation("单条查询接口")
    public JSONResult get(@PathVariable("id")Long id){
        return JSONResult.success(departmentService.selectById(id));
    }


    /**
    * 查询所有对象
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    @ApiOperation("查询所有对象")
    public JSONResult list(){
        List<Department> list = departmentService.selectList(null);
        return JSONResult.success(list);
    }


    /**
    * 带条件分页查询数据
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    @ApiOperation("分页接口")
    public JSONResult page(@RequestBody DepartmentQuery query){
        //高级查询的替换字段
        EntityWrapper<Department> queryWrapper = new EntityWrapper<>();
        queryWrapper.like("sn",query.getKeyword());
        Page<Department> page = new Page<Department>(query.getPage(),query.getRows());
        //将替换的条件传入到selectPage方法中间
        page = departmentService.selectPage(page,queryWrapper);
        //domain中添加的字段去foreach循环拿到employee的映射的关系
        List<Department> records = page.getRecords();
        for (Department department :records ) {
            //查询员工的管理
            Long managerId = department.getManagerId();
            Employee emp = employeeService.selectById(managerId);
            department.setManager(emp);
            //查询上级部门的映射的关系
            Long parentId = department.getParentId();
            Department department1 = departmentService.selectById(parentId);
            department.setParent(department1);
        }

        return JSONResult.success(new PageList<Department>(page.getTotal(),page.getRecords()));

    }

    /**
     * tree树形结构
     */
    @RequestMapping(value = "/tree",method = RequestMethod.GET)
    @ApiOperation("部门树的接口")
    public List<Department> tree(){
        return departmentService.tree();

    }
    /**
     * 批量删除
     */
    @RequestMapping(value="/ids",method=RequestMethod.POST)
    @ApiOperation("删除接口")
    public JSONResult deleteids(@RequestBody List<Long> ids){
        departmentService.deleteByIds(ids);
        return JSONResult.success();
    }

}
